*     ***************************************************************** *;
*     ***************************************************************** *;
*       File-Name:      main.do                                     	*;
*       Date:           09/04/2020                                      *;
*       Authora:        Amodio, Baccini, Chiovelli, Di Maio             *;
*       Purpose:        Replication of "Agricultural Comparative        *;
*                       Advantage and Legislators' Support for Trade    *;
*                       Agreements,"                                    *;                                                                      
*       Input Files:    main_dataset.dta 		&				        *;
*						anes_dataset.dta 		        				*; 
*       Output Files:   JOP_logfile_main        &                       *;
*                       JOP_logfile_anes                                *:
*       Machine:        Office                                          *;
*       Program: 		Stata 15                                        *;
*     ****************************************************************  *;
*     ****************************************************************  *;

clear
clear matrix
clear mata
set maxvar 50000
set matsize 11000

cd "C:\Users\lbacci\Dropbox\Long_Term_Projects\Giorgio_Jeff_Results\Paper\Submission\Revisions\Analysis\Definitive"
use anes_dataset, replace
log using JOP_logfile_anes.log
set more off 


*Relabel main variables
rename trade_shock China_shock
label variable China_shock ""
rename ext1_x1y1 CA_export
rename ext2_x2y2 CA_import
*rename ratio_1_sum_stdz CA_export
*rename ratio_2_sum_stdz CA_import
*rename p_ratio_1_sum_stdz CA_export
*rename p_ratio_2_sum_stdz CA_import
*rename w_ext1_x1y1 CA_export
*rename w_ext2_x2y2 CA_import

*Take the log of the two main covariates
gen lnCA_export=ln(CA_export+0.0001)
gen lnCA_import=ln(CA_import+0.0001)

** new measures Sean 
gen ln_measure_5_wld_crops_Excl_Brd =ln(measure_5_wld_crops_Excl_Brd)
gen ln_measure_5_wld_crops_Incl_Brd=ln(measure_5_wld_crops_Incl_Brd)
gen ln_measure_5_wld_agri_Excl_Brd=ln(measure_5_wld_agri_Excl_Brd)
gen ln_measure_5_wld_agri_Incl_Brd=ln(measure_5_wld_agri_Incl_Brd)
gen ln_measure_5_wld_manu_Incl_Brd=ln(measure_5_wld_manu_Incl_Brd) 
gen ln_measure_5_wld_manu_Excl_Brd=ln(measure_5_wld_manu_Excl_Brd)
gen lnservice_ratio_5yr=ln( service_ratio_5yr )

gen ln_measure_5_bi_manu_Incl_Brd=ln(measure_5_bi_manu_Incl_Brd)
gen ln_measure_5_bi_agri_Incl_Brd=ln(measure_5_bi_agri_Incl_Brd)
gen ln_measure_5_bi_crops_Incl_Brd=ln(measure_5_bi_crops_Incl_Brd) 


rename ln_measure_5_wld_manu_Incl_Brd p_tot_manu
rename ln_measure_5_wld_agri_Incl_Brd p_tot_agri
rename ln_measure_5_wld_crops_Incl_Brd p_tot_crops
rename lnservice_ratio_5yr p_tot_serv
label variable p_tot_manu "total export weighted by share of workers (manu)"
label variable p_tot_agri "total export weighted by share of workers (agri)"
label variable p_tot_crops "total export weighted by share of workers (crops)"
label variable p_tot_serv "total service production weighted by share of workers (service)"
gen p_ratio_tot_manu_exog=lnCA_export-p_tot_manu
gen p_ratio_tot_serv_exog=lnCA_export-p_tot_serv
gen p_ratio_tot_manu_exog1=p_tot_agri-p_tot_manu
gen p_ratio_tot_manu_exog2=p_tot_crops-p_tot_manu

gen ln_bal_bilat_5_in=ln(bal_bilat_5_in) if bal_bilat_5_in>0
gen ln_bal_bilat_5_in_neg=ln(-bal_bilat_5_in) if bal_bilat_5_in<0
replace ln_bal_bilat_5_in=-ln_bal_bilat_5_in_neg if bal_bilat_5_in<0
rename ln_bal_bilat_5_in p_bi_manu

egen id_district=group( fips_dist )

gen farmer=1 if occupation_14==9
*replace farmer=1 if profile_occ3current==6
*replace farmer=1 if profile_occ3recent==6
replace farmer=0 if farmer==. & occupation_14!=. 


*preserve
collapse (mean) p_ratio_tot_manu_exog p_ratio_tot_manu_exog1 p_ratio_tot_manu_exog2 p_ratio_tot_serv_exog offshorable democratic p_bi_manu lnCA_export limit_imports gender educ_7 educ_6 ideo_respondent age race_4 income_group occupation_group party_id_7 weight_ftf_0 weight_ftf_1 weight_ftf_2 weight_web_0 weight_web_1 weight_web_2 weight_full_0 weight_full_1 weight_full_2 age_group farmer working_middle work_status_7 race_7 race_3 class_8 (first) state_fips district partner, by( individual_id id_district anes_year)
egen id_state=group(district)

*Label
label variable lnCA_export "Agri CA"
label variable p_ratio_tot_manu_exog "Agri CA1"
label variable p_ratio_tot_manu_exog1 "Agri CA2"
label variable limit "Support For Free Trade"

*Table 2
*1
xi: reg limit lnCA_export democratic offshorable gender educ_6 ideo_resp age i.id_district i.anes_year, cluster( id_district ) r
outreg2 using table2.xls, bdec(4) tdec(2) keep(lnCA_export) label addtext(District FE, Yes, Wave FE, Yes, Main Controls, Yes, Individual Controls, Yes) append
margins, atmeans  at((p25) lnCA_export)  at((p75) lnCA_export)
gen e1=1.418768/1.330474
*2
xi: reg limit p_ratio_tot_manu_exog democratic offshorable gender educ_6 ideo_resp age i.id_district i.anes_year, cluster( id_district ) r
outreg2 using table2.xls, bdec(4) tdec(2) keep(p_ratio_tot_manu_exog) label addtext(District FE, Yes, Wave FE, Yes, Main Controls, Yes, Individual Controls, Yes) append
margins, atmeans  at((p25) p_ratio_tot_manu_exog)  at((p75) p_ratio_tot_manu_exog)
gen e2=1.423982/1.325954
*3
xi: reg limit p_ratio_tot_manu_exog1 democratic offshorable gender educ_6 ideo_resp age i.id_district i.anes_year, cluster( id_district ) r
outreg2 using table2.xls, bdec(4) tdec(2) keep(p_ratio_tot_manu_exog1) label addtext(District FE, Yes, Wave FE, Yes, Main Controls, Yes, Individual Controls, Yes) append
margins, atmeans  at((p25) p_ratio_tot_manu_exog1)  at((p75) p_ratio_tot_manu_exog1)
gen e3=1.383905/1.362283


##### APPENDIX

*Table B2

xi: reg limit lnCA_export democratic offshorable gender educ_6 ideo_resp age i.id_district i.anes_year, cluster( id_district ) r
sum limit lnCA_export p_ratio_tot_manu_exog p_ratio_tot_manu_exog1 democratic offshorable gender educ_6 ideo_resp age id_district anes_year if e(sample)

*Table C2
*1
xi: reg limit lnCA_export i.id_district i.anes_year, cluster( id_district ) r
outreg2 using tableC2.xls, bdec(4) tdec(2) e(rmse) keep(lnCA_export) label addtext(District FE, Yes, Wave FE, Yes, Main Controls, No, Individual Controls, No) 
*2
xi: reg limit p_ratio_tot_manu_exog i.id_district i.anes_year, cluster( id_district ) r
outreg2 using tableC2.xls, bdec(4) tdec(2) e(rmse) keep(p_ratio_tot_manu_exog) label addtext(District FE, Yes, Wave FE, Yes, Main Controls, No, Individual Controls, No) append
*3
xi: reg limit p_ratio_tot_manu_exog1 i.id_district i.anes_year, cluster( id_district ) r
outreg2 using tableC2.xls, bdec(4) tdec(2) e(rmse) keep(p_ratio_tot_manu_exog1) label addtext(District FE, Yes, Wave FE, Yes, Main Controls, No, Individual Controls, No) append
*4
xi: reg limit lnCA_export democratic offshorable i.id_district i.anes_year, cluster( id_district ) r
outreg2 using tableC2.xls, bdec(4) tdec(2) e(rmse) keep(lnCA_export) label addtext(District FE, Yes, Wave FE, Yes, Main Controls, Yes, Individual Controls, No) append
*5
xi: reg limit p_ratio_tot_manu_exog democratic offshorable i.id_district i.anes_year, cluster( id_district ) r
outreg2 using tableC2.xls, bdec(4) tdec(2) e(rmse) keep(p_ratio_tot_manu_exog) label addtext(District FE, Yes, Wave FE, Yes, Main Controls, Yes, Individual Controls, No) append
*6
xi: reg limit p_ratio_tot_manu_exog1 democratic offshorable i.id_district i.anes_year, cluster( id_district ) r
outreg2 using tableC2.xls, bdec(4) tdec(2) e(rmse) keep(p_ratio_tot_manu_exog1) label addtext(District FE, Yes, Wave FE, Yes, Main Controls, Yes, Individual Controls, No) append
*7
xi: reg limit lnCA_export democratic offshorable gender educ_6 ideo_resp age i.id_district i.anes_year, cluster( id_district ) r
outreg2 using tableC2.xls, bdec(4) tdec(2) e(rmse) keep(lnCA_export) label addtext(District FE, Yes, Wave FE, Yes, Main Controls, Yes, Individual Controls, Yes) append
*8
xi: reg limit p_ratio_tot_manu_exog democratic offshorable gender educ_6 ideo_resp age i.id_district i.anes_year, cluster( id_district ) r
outreg2 using tableC2.xls, bdec(4) tdec(2) e(rmse) keep(p_ratio_tot_manu_exog) label addtext(District FE, Yes, Wave FE, Yes, Main Controls, Yes, Individual Controls, Yes) append
*9
xi: reg limit p_ratio_tot_manu_exog1 democratic offshorable gender educ_6 ideo_resp age i.id_district i.anes_year, cluster( id_district ) r
outreg2 using tableC2.xls, bdec(4) tdec(2) e(rmse) keep(p_ratio_tot_manu_exog1) label addtext(District FE, Yes, Wave FE, Yes, Main Controls, Yes, Individual Controls, Yes) append

log close
